import { Button } from 'ant-design-vue'
import { omit } from 'lodash-es'
import { defineComponent, ref } from 'vue'

import { marketingApi } from '@/apis'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { download } from '@/utils'

export default defineComponent({
  name: 'ExchangeRecord',
  setup() {
    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        toolbarConfig: {
          slots: {
            buttons: 'toolbarButtons'
          }
        },
        formConfig: {
          items: [
            {
              title: '兑换时间',
              field: 'date',
              itemRender: {
                name: TABLE_RENDERER.FormItemDate,
                props: {
                  type: 'range',
                  placeholder: ['开始时间', '结束时间'],
                  valueFormat: 'YYYY-MM-DD',
                  keys: ['exchangeStartTime', 'exchangeEndTime']
                }
              }
            },
            {
              title: '关键字',
              field: 'keyword',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '昵称或手机号'
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columns: [
          {
            title: '客户',
            field: 'user'
          },
          {
            title: '商品名称',
            field: 'goodsName'
          },
          {
            title: '积分价',
            field: 'point'
          },
          {
            title: '数量',
            field: 'quantity'
          },
          {
            title: '兑换时间',
            field: 'exchangeTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss']
          },
          {
            title: '兑换码',
            field: 'exchangeCode'
          }
        ],
        pagerConfig: {
          pageSize: 15
        },
        proxyConfig: {
          seq: true,
          form: true,
          props: {
            result: 'dataList',
            total: 'page.totalCount'
          },
          ajax: {
            query: async ({ page, form }) => {
              form = omit(form, ['date'])

              const data = await marketingApi.getExchangeRecordList({
                pageParameter: {
                  pageSize: page.pageSize,
                  currentPage: page.currentPage
                },
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    async function handleExport() {
      const { form } = tableRef.value.table.getProxyInfo()!

      try {
        tableRef.value.showLoading()
        const res = await marketingApi.exportExchangeRecordList(form)
        download(res, '兑换记录列表')
      } finally {
        tableRef.value.hideLoading()
      }
    }

    return {
      tableRef,
      tableConfig,
      handleExport
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            toolbarButtons: () => (
              <Button onClick={this.handleExport}>导出</Button>
            )
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
